/******************************************************文本框*****************************************************/
_$.FileUploadLogo=function (target){
    var _this = this;
    _$.FileUploadLogo.superclass.constructor.call(_this,target);
};
_$.regClass("FileUploadLogo",_$.FileUploadLogo);

_$.extend(_$.FileUploadLogo,_$.FormItem, {
    _cls:_$._clsPre + "FileUploadLogo",
    _uiCls:_$._uiPreCls + "FileUploadLogo",
    _attrProps:function(){
        var _this = this;
        var properties = _$.FileUploadLogo.superclass._attrProps.call(_this);
        properties.addAll(["systemCode","moduleId","bizBase","onchange"]);
        return properties;
    },
    _attrOpts:function(){
        var _this = this;
        var opts = _$.FileUploadLogo.superclass._attrOpts.call(_this);
        return $.extend(true,opts,{
            "domainUrl":"http://oyxr5nbal.bkt.clouddn.com/",
            "getTokenUrl":"/base/file/getuploadToken",
            "defaultImg":"/base/images/timg.jpg",
            "domain":"/mytest",
            "systemCode":"base",
            "moduleId":"system",
            "bizBase":""
        });
    },
    _editModel:function(){
        var _this = this;
        _this._clearShow();
        var defaultImg = _this.getOption("defaultImg");
        var source='<div class="input-file" type="button"  onclick="">' +
            '<img class="showImage" src="'+defaultImg+'" />' +
            '<input type="file" style="height:50px;width:100%;" id="logo_'+_this.id+'" />' +
            '<div  class="progress" style="display: none;width: 200px;">' +
            '<div  class="progressBar" aria-valuenow="0" aria-valuemax="100" aria-valuemin="0" style="width:0%;">' +
            '</div></div></div>';

        _this._replaceNode(source);
        _this.component = $(".showImage",_this.el);
        _this.progress = $(".progress",_this.el);
        _this.progressBar = $(".progressBar",_this.el);
        _this.el.addClass(_this._uiCls);
        _this._initUpload();
        _this._setStyle();
        _this.setOption("model",_EDITABLE_);
        _this._init();
        _this._bindEvents();
    },
    _showModel:function(){
        var _this = this;
        _this._clearEdit();
        var defaultImg = _this.getOption("defaultImg");
        var source='<div class="showDiv"><img class="showImage" src="'+defaultImg+'"/></div>';
        _this._replaceNode(source);
        _this.component = $(".showImage",_this.el);
        _this.el.addClass(_this._uiCls);
        _this.el.addClass(_this._spanCls);
        _this.setOption("model",_SHOW_);
        _this._init();
    },
    _initUpload:function(){
        var _this = this;
        var getTokenUrl = _this.getOption("getTokenUrl");
        var domain = _this.getOption("domain");
        var option = {
            runtimes : "html5,html4", // 上传模式，依次退化
            browse_button : "logo_" + _this.id, // 上传选择的点选按钮，必需
            uptoken_url :getTokenUrl, // Ajax请求uptoken的Url，强烈建议设置（服务端提供）
            get_new_uptoken : false, // 设置上传文件的时候是否每次都重新获取新的uptoken
            domain :domain, // bucket域名，下载资源时用到，必需
            max_file_size : "100mb", // 最大文件体积限制
            max_retries : 2, // 上传失败最大重试次数
            chunk_size : "4mb", // 分块上传时，每块的体积
            auto_start : true, // 选择文件后自动上传，若关闭需要自己绑定事件触发上传
            init : {
                Key : key,
                'FileUploaded' : function(up, file, info) {
                    _this.progress.fadeOut();
                    var currentFileName = eval("(" + info + ")").key;

                    var domainUrl = _this.getOption("domainUrl");
                    var callUrl = domainUrl + currentFileName + "?" +new Date().getTime();

                    var systemCode = _this.getOption("systemCode");
                    var moduleId = _this.getOption("moduleId");
                    var bizBase = _this.getOption("bizBase");

                    var f = {
                        "systemCode":systemCode,
                        "moduleId":moduleId,
                        "bizBase":bizBase,
                        "origiName" : file.name,
                        "currentFileName" : currentFileName,
                        "callUrl" : callUrl,
                        "fileType" : file.type,
                        "fileSize" : _this.getSize(file.size)
                    };
                    _this.setValue(JSON.stringify([f]));
                },
                'FilesAdded' : function(up, files) {},
                'BeforeUpload' : function(up, file) {
                    _this.progress.fadeIn();
                },
                'UploadProgress' : function(up, file) {
                    _this.progressBar.css("width", file.percent + "%");
                },
                'Error' : function(up, err, errTip) {
                    _this.progress.fadeOut();
                    console.log(up);
                    console.log(err);
                    console.log(errTip);
                    alert("上传文件出错");
                }
            }
        };
        var qiniuInstance = new QiniuJsSDK();
        var uploader = qiniuInstance.uploader(option);
    },
    getSize:function(size) {
        if (size < 1000) {
            return size + " BYTE";
        }
        size = (size / 1000).toFixed(2);
        if (size < 1000) {
            return size + " KB";
        }
        size = (size / 1000.0).toFixed(2);
        return size + " MB";
    },
    setValue:function(value){
        var _this = this;
        if(isEmpty(value)){
            _this.value = null;
            _this.component.attr("src",defaultImg);
        }else{
            _this.value = value;
            var arr = JSON.parse(value);
            if(arr.length>0){
                var f = arr[0];
                var imgUrl;
                if (f.fileType.indexOf("image") > -1) {
                    imgUrl = f.callUrl;
                } else {
                    var defaultImg = _this.getOption("defaultImg");
                    imgUrl = defaultImg;
                }
                _this.component.attr("src",imgUrl);

            }else{
                _this.value = null;
                _this.component.attr("src",defaultImg);
            }
        }
    },
    getValue:function(){
        var _this = this;
        return _this.value;
    }
});
